package com.yanqu.road.server.cmd.activity.toyhouse;

import com.yanqu.road.entity.activity.toyhouse.ToyHouseBlock;
import com.yanqu.road.entity.activity.toyhouse.ToyHouseBlockData;
import com.yanqu.road.entity.activity.toyhouse.ToyHouseUserData;
import com.yanqu.road.entity.activity.toyhouse.config.ToyHouseConfig;
import com.yanqu.road.entity.activity.toyhouse.eliminate.ToyHouseEliminateTips;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ToyHouseProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.toyhouse.ToyHouseModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.toyhouse.ToyHouseEliminateLogic;
import com.yanqu.road.server.manger.activity.toyhouse.ToyHouseMgr;
import com.yanqu.road.server.pb.ToyHousePb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_TOY_HOUSE_GET_TIPS, desc = "获取消除提示")
public class ToyHouseGetTipsCmd extends AbstractCommand {

    private int multi;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ToyHouseProto.ToyHouseGetEliminateTipsReqMsg reqMsg = ToyHouseProto.ToyHouseGetEliminateTipsReqMsg.parseFrom(packet.getBytes());
        multi = reqMsg.getMulti();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 活动配置
        ToyHouseConfig config = ToyHouseMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_TOY_HOUSE_NO_ACTIVITY;
            return;
        }

        // 玩家数据
        ToyHouseUserData userData = player.getModule(ToyHouseModule.class).getUserData(config);
        if (userData == null) {
            ret = GameErrorCode.E_TOY_HOUSE_NO_UNLOCK;
            return;
        }

        // 多倍
        ToyHouseBlockData blockData = userData.getBlockDataMap().get(multi);
        if (!config.getMultiUnlockMap().containsKey(multi) || blockData == null) {
            ret = GameErrorCode.E_TOY_HOUSE_NO_MULTI;
            return;
        }

        ToyHouseEliminateTips tips = ToyHouseEliminateLogic.calToyHouseEliminateTips(blockData.getBlockList());
        if (tips.isSuccess()) {
            ToyHouseProto.ToyHouseGetEliminateTipsRespMsg.Builder builder = ToyHouseProto.ToyHouseGetEliminateTipsRespMsg.newBuilder();
            builder.setRet(0);
            builder.setFromBlock(ToyHousePb.parseToyHouseBlockMsg(tips.getFrom()));
            if (tips.getTo() != null) {
                builder.setToBlock(ToyHousePb.parseToyHouseBlockMsg(tips.getTo()));
            }
            for (ToyHouseBlock toyHouseBlock : tips.getBlockList()) {
                builder.addBlockList(ToyHousePb.parseToyHouseBlockMsg(toyHouseBlock));
            }
            player.sendPacket(ClientProtocol.U_TOY_HOUSE_GET_TIPS, builder);
        } else {
            log.error("ToyHouse calToyHouseEliminateTips false on ToyHouseGetTipsCmd! activityId={}, userId={}, rearrange!", userData.getActivityId(), userData.getUserId());
            int[][] copyBlockList = ToyHouseMgr.getCopyBlockList(blockData.getBlockList());
            ToyHouseEliminateLogic.rearrange(copyBlockList);
            userData.getBlockDataMap().put(multi, new ToyHouseBlockData(multi, copyBlockList));
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ToyHouseProto.ToyHouseGetEliminateTipsRespMsg.Builder builder = ToyHouseProto.ToyHouseGetEliminateTipsRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_TOY_HOUSE_GET_TIPS, builder);
        }
    }
}
